#include <Arduino.h>
#include <IRremoteESP8266.h>
#include <IRrecv.h>
#include <IRutils.h>

#include <WS2812FX.h>
#include <EEPROM.h>
 

// An IR detector/demodulator is connected to GPIO pin 14(D5 on a NodeMCU
// board).
const uint16_t kRecvPin = 5;

IRrecv irrecv(kRecvPin);

decode_results results;



#define LED_COUNT 510
#define LED_PIN 14

#define TIMER_MS 10000
#define MODE_EEPROM_ADDRESS  0


// Parameter 1 = number of pixels in strip
// Parameter 2 = Arduino pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
//   NEO_RGBW    Pixels are wired for RGBW bitstream (NeoPixel RGBW products)
WS2812FX ws2812fx = WS2812FX(LED_COUNT, LED_PIN, NEO_RGB + NEO_KHZ800);

unsigned long last_change = 0;
unsigned long now = 0;
bool Power = true;
char modeVar = 0;

//  now = millis();
//
//  ws2812fx.service();
//
//  if(now - last_change > TIMER_MS) {
//    ws2812fx.setMode((ws2812fx.getMode() + 1) % ws2812fx.getModeCount());
//    last_change = now;
//  }

void setup() {
  byte value;
  Serial.begin(115200);
   EEPROM.begin(512);
    Serial.println();
  Serial.println();
  Serial.println();
  irrecv.enableIRIn();  // Start the receiver
  while (!Serial)  // Wait for the serial connection to be establised.
    delay(50);
  Serial.println();
  Serial.print("IRrecvDemo is now running and waiting for IR message on Pin ");
  Serial.println(kRecvPin);

  value = EEPROM.read(MODE_EEPROM_ADDRESS);
  if(value > 59)
  {
    EEPROM.write(MODE_EEPROM_ADDRESS, 0);
    if (EEPROM.commit()) {
      Serial.println("EEPROM successfully committed");
    } else {
      Serial.println("ERROR! EEPROM commit failed");
    }
  }else
  {
    modeVar = value;
  }
  
  ws2812fx.init();
  //0~255
  ws2812fx.setBrightness(255);
  //10 =非常快，5000 =非常慢 
  ws2812fx.setSpeed(10);
  ws2812fx.setColor(ORANGE);
  ws2812fx.setMode(modeVar);
  ws2812fx.stop();
}

void loop() {
  if (irrecv.decode(&results)) {
    switch(results.value)
    {
      case 0xFFA25D: Power = ~Power ; 
                     Serial.println("Power");
                     if(Power) {
                      ws2812fx.setMode(modeVar);
                      ws2812fx.setColor(RED);
                      ws2812fx.start();
                     }
                     else 
                     {
                         ws2812fx.setMode(4);                        
                     }
                     break;  //电源键
                     
      case 0xFF22DD:
                     Serial.println("modeVar--"); 
                     if(Power)
                     {
                       modeVar--; 
                       
                       if(modeVar < 0) modeVar = 55;
                        Serial.print("modeVar = "); 
                        Serial.print(modeVar,DEC); 
                        Serial.print("\n");
                       ws2812fx.setMode(modeVar);
                     }
                     
                     break;  //上一个动画
      case 0xFFC23D: 
                      Serial.println("modeVar++"); 
                      if(Power)
                     {
                       modeVar++; 
                      
                       if(modeVar > 55) modeVar = 0;
                        Serial.print("modeVar = "); 
                        Serial.print(modeVar,DEC); 
                        Serial.print("\n");
                       ws2812fx.setMode(modeVar);
                     }
      
                     break;  //下一个动画
      case 0xFF629D: 
                     Serial.println("increaseSpeed");
                     if(Power)
                     {
                       ws2812fx.increaseSpeed(1000);
                       ws2812fx.start();
                       Serial.print("modeVar = "); 
                       Serial.print(ws2812fx.getSpeed(),DEC); 
                       Serial.print("\n");
                     }
                     break;  //动画速度加
      case 0xFFA857: 
                      Serial.println("decreaseSpeed");
                     if(Power)
                     {
                       ws2812fx.decreaseSpeed(1000);
                       ws2812fx.start();
                       Serial.print("modeVar = "); 
                       Serial.print(ws2812fx.getSpeed(),DEC); 
                       Serial.print("\n");
                     }
                     break;  //动画速度减
      case 0xFFE01F: 
                     Serial.println("increaseBrightness");
                     if(Power)
                     {
                       ws2812fx.increaseBrightness(50);
                     }
                     break;  //亮度减
      case 0xFF906F: 
                      Serial.println("decreaseBrightness");
                     if(Power)
                     {
                       ws2812fx.decreaseBrightness(50);
                     }
                     break;  //亮度加
    }   
    irrecv.resume();  // Receive the next value
  }
  delay(300);
  ws2812fx.service();
}
